package qsort

func quicksort(values []int, left, right int) {
    temp := values[left]
    l, r := left, right
    for l < r {
        for l < r && temp <= values[r] {
            r--
        }

        if l < r {
            values[l] = values[r]
            l++
        }

        for l < r && temp >= values[l] {
            l++
        }

        if l < r {
            values[r] = values[l]
            r--
        }
    }
    values[l] = temp

    if l - left > 1 {
        quicksort(values, left, l - 1)
    }

    if right - l > 1 {
        quicksort(values, l + 1, right)
    }
}

func QuickSort(values []int) {
    quicksort(values, 0, len(values) - 1)
}
